/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

/**
 * This class represents a geometric vector.
 * @author Mentors
 */
public class Vector2D {
    public double x,y;
    public static final Vector2D scratch = new Vector2D();
    public static final Vector2D scratch2 = new Vector2D();
    
    public Vector2D(){
        setTo(0,0);
    }
    
    public Vector2D(double x,double y){
        setTo(x,y);
    }
    
    public Vector2D(Vector2D v){
        this(v.x,v.y);
    }
    
    public final void setTo(double x,double y){
        this.x = x;
        this.y = y;
    }
    
    public final void setTo(Vector2D v){
        setTo(v.x,v.y);
    }
    
    public void add(Vector2D v){
        x += v.x;
        y += v.y;
    }
    
    public void subtract(Vector2D v){
        x -= v.x;
        y -= v.y;
    }
    
    public void scale(double scalar){
        x *= scalar;
        y *= scalar;
    }
    
    public double getLength(){
        return Math.sqrt(x * x + y * y);
    }
    
    public void normalize(){
        divide(getLength());
    }
    
    public void divide(double scalar){
        x /= scalar;
        y /= scalar;
    }
    
    public double getDotProduct(Vector2D v){
        return x * v.x + y * v.y;
    }
    
    public String toString(){
        return "Vector: [" + x + "," + y + "]";
    }
}
